<template>
  <a-card class="cardShadow">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :rules="validatorRules">
        <!--        <a-form-item-->
        <!--          :labelCol="labelCol"-->
        <!--          :wrapperCol="wrapperCol"-->
        <!--          label="年份">-->
        <!--          <a-input v-decorator="['year', {-->
        <!--            rules: [-->
        <!--              {-->
        <!--                required: true,-->
        <!--                message: '年份不能为空!',-->
        <!--              },-->
        <!--            ],-->
        <!--            }]" placeholder="请输入年份" suffix="年" />-->
        <!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="能源消费总量">
          <a-input v-decorator="['totalEnergyConsumption', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入能源消费总量" suffix="10⁴tce" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预测能源消费总量">
          <a-input v-decorator="['energyConsumptionIntensity', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入预测能源消费总量" suffix="10⁴tce" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="碳排放总量">
          <a-input v-decorator="['totalCarbonEmissions', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入碳排放总量" suffix="10⁴tco²" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预测碳排放总量">
          <a-input v-decorator="['carbonEmissionIntensity', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入预测碳排放总量" suffix="10⁴tco²" />
        </a-form-item>
      </a-form>
      <a-button :loading="confirmLoading" class="button" icon="upload" type="primary" @click="handleOk()">提交</a-button>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { getAction, postAction } from '@api/manage'
import Vue from 'vue'
import { LACK } from '@/store/mutation-types'

export default {
  name: 'yearAccumulated',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      dialogStyle: {
        top: '200px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      validatorRules: {
        totalEnergyConsumption: {
          rules: [{ required: true, trigger: 'change', message: '请输入数字！', pattern: '^\d{1,}\.\d{1,}|\d{1,}]/g' }]
        }
      },
      url: {
        add: '/carbon/carbonOverviewYearData/add',
        edit: '/carbon/carbonOverviewYearData/edit'
      }
    }
  },
  created() {
  },
  mounted() {
    this.getData()
  },
  methods: {

    getData() {
      let year = String(new Date().getFullYear())
      getAction('/carbon/carbonOverviewYearData/list', { year: year, areaCode: '021' }).then((res) => {
        if (res.success) {
          console.log(res)
          this.model = res.result.records[0]
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(
              this.model,
              'year',
              'totalEnergyConsumption',
              'totalEnergyConsumptionYoy',
              'totalCarbonEmissions',
              'totalCarbonEmissionsYoy',
              'energyConsumptionIntensity',
              'energyConsumptionIntensityYoy',
              'carbonEmissionIntensity',
              'carbonEmissionIntensityYoy'))
          })
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model) {
            httpurl += this.url.add
          } else {
            httpurl += this.url.edit
          }
          console.log(httpurl)
          let formData = Object.assign(values)
          formData.areaCode = '021'
          if (!formData.year) {
            let year = String(new Date().getFullYear())
            formData.year = year
          }
          postAction(httpurl, formData).then(res => {
            console.log(res)
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('refresh')
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
.button {
  margin-left: 50%;
  transform: translateX(-50%);
}

.cardShadow {
//  整个部分看起来悬浮起来的阴影

}
</style>